package com.cjst.lot.runway;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cjst.pojo.runway.AgeGroup;
import com.cjst.pojo.runway.Numbers;
import com.cjst.pojo.runway.SexGroup;
import com.cjst.pojo.runway.SportDataByPark;
import com.cjst.pojo.runway.SportSranking;
import com.cjst.pojo.runway.Stram;
import com.cjst.pojo.runway.ThirtyDatShiStory;
import com.cjst.pojo.runway.TimeStream;
import com.cjst.util.Base64Util;
import com.cjst.util.common.HttpXmlClient;
import com.cjst.util.json.JsonUtil;

@Component
public class RunWayOperation {
	private static ApplicationContext applicationContext;
    public static void setApplicationContext(ConfigurableApplicationContext context) {
    	RunWayOperation.applicationContext = context;
    }
    /**
     * 得到整体数据
     * @param equipmentid
     * @param start
     * @param end
     * @param type
     * @return
     */
	public Object returnResult(String equipmentid,String start,String end,Integer type){
		if(type==null){
			type=0;
		}
		String url=RunWayUtil.URL;
		if(type==0){							//注册人数
			url=url+RunWayUtil.NUMBER+"?"+"equipmentid="+equipmentid;
			String result=HttpXmlClient.get(url);
			//System.out.println(result);
			JSONObject jsonObject = JSONObject.parseObject(result);
			int code=jsonObject.getIntValue("code");
			if(code==200){
				String strData=jsonObject.getString("data");
				if(strData!=null){
					JSONObject dt = JSONObject.parseObject(strData);
					Numbers nm=new Numbers();
					nm.setYesterdayNum(dt.getInteger("yesterdayNum"));
					nm.setTodayNum(dt.getInteger("todayNum"));
					nm.setSumNum(dt.getInteger("sumNum"));
					return nm;
				}
			}else{
				return null;
			}
		}else if(type==1){						//查询公园注册用户年龄段分步
			url=url+RunWayUtil.AGEGROUP+"?"+"equipmentid="+equipmentid+"&start="+start+"&end="+end;
			String result=HttpXmlClient.get(url);
			//System.out.println(result);
			JSONObject jsonObject = JSONObject.parseObject(result);
			int code=jsonObject.getIntValue("code");
			if(code==200){
				String strData=jsonObject.getString("data");
				if(strData!=null){
					List<AgeGroup> ageList=this.getAge(strData);
					return ageList;
				}
			}else{
				return null;
			}
		}else if(type==2){						//查询公园注册用户性别分步
			url=url+RunWayUtil.SEXGROUP+"?"+"equipmentid="+equipmentid+"&start="+start+"&end="+end;
			String result=HttpXmlClient.get(url);
			JSONObject jsonObject = JSONObject.parseObject(result);
			int code=jsonObject.getIntValue("code");
			if(code==200){
				String strData=jsonObject.getString("data");
				if(strData!=null){
					try {
						SexGroup sg = JsonUtil.json2pojo(strData, SexGroup.class);
						return sg;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return null;
					}
				}
			}else{
				return null;
			}
		}else if(type==3){					//查询摄像头点位人流量
			url=url+RunWayUtil.STREAM+"?"+"equipmentid="+equipmentid+"&start="+start+"&end="+end;
			String result=HttpXmlClient.get(url);
			//System.out.println(result);
			JSONObject jsonObject = JSONObject.parseObject(result);
			int code=jsonObject.getIntValue("code");
			if(code==200){
				String strData=jsonObject.getString("data");
				if(strData!=null){
					try {
						List<Stram> sList=JsonUtil.jsonlists(strData, Stram.class);
						return sList;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return null;
					}
				}
			}else{
				return null;
			}
		}else if(type==4){						//查询摄像头时间段人流量
			url=url+RunWayUtil.TIMESTREAM+"?"+"equipmentid="+equipmentid+"&start="+start+"&end="+end;
			String result=HttpXmlClient.get(url);
			//System.out.println(result);
			JSONObject jsonObject = JSONObject.parseObject(result);
			int code=jsonObject.getIntValue("code");
			if(code==200){
				String strData=jsonObject.getString("data");
				if(strData!=null){
					try {
						List<TimeStream> sList=JsonUtil.jsonlists(strData, TimeStream.class);
						return sList;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return null;
					}
				}
			}else{
				return null;
			}
		}else if(type==5){						//查询公园实时周运动排行榜
			url=url+RunWayUtil.SPORTSRANKING+"?"+"equipmentid="+equipmentid;
			String result=HttpXmlClient.get(url);
			//System.out.println(result);
			JSONObject jsonObject = JSONObject.parseObject(result);
			int code=jsonObject.getIntValue("code");
			if(code==200){
				String strData=jsonObject.getString("data");
				if(strData!=null){
					try {
						List<SportSranking> sList=JsonUtil.jsonlists(strData, SportSranking.class);
						sList.sort((x, y) -> Integer.compare(x.getDistance(), y.getDistance()));
						if(sList!=null && sList.size()>0){
							for (SportSranking sportSranking : sList) {
								sportSranking.setUserImage(RunWayUtil.IMGURL+sportSranking.getUserImage());
							}
						}
						Collections.reverse(sList); 
						return sList;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return null;
					}
				}
			}else{
				return null;
			}
		}else if(type==6){						//查询公园设备在线状态
			url=url+RunWayUtil.ONLINESTATE+"?"+"equipmentid="+equipmentid;
		}
		
		return HttpXmlClient.get(url);
	}
	public List<AgeGroup> getAge(String strData){
		List<AgeGroup> ageList=new ArrayList<AgeGroup>();
		JSONObject dt = JSONObject.parseObject(strData);
		AgeGroup ag1=new AgeGroup();
		ag1.setAge("0-10");
		ag1.setNum(dt.getIntValue("0010"));
		ageList.add(ag1);
		
		AgeGroup ag2=new AgeGroup();
		ag2.setAge("10-15");
		ag2.setNum(dt.getIntValue("1015"));
		ageList.add(ag2);
		
		AgeGroup ag3=new AgeGroup();
		ag3.setAge("15-25");
		ag3.setNum(dt.getIntValue("1525"));
		ageList.add(ag3);
		
		AgeGroup ag4=new AgeGroup();
		ag4.setAge("25-35");
		ag4.setNum(dt.getIntValue("2535"));
		ageList.add(ag4);
		
		AgeGroup ag5=new AgeGroup();
		ag5.setAge("35-45");
		ag5.setNum(dt.getIntValue("3545"));
		ageList.add(ag5);
		
		AgeGroup ag6=new AgeGroup();
		ag6.setAge("45-55");
		ag6.setNum(dt.getIntValue("4555"));
		ageList.add(ag6);
		
		AgeGroup ag7=new AgeGroup();
		ag7.setAge("55-65");
		ag7.setNum(dt.getIntValue("5565"));
		ageList.add(ag7);
		
		AgeGroup ag8=new AgeGroup();
		ag8.setAge("65以上");
		ag8.setNum(dt.getIntValue("6500"));
		ageList.add(ag8);
		return ageList;
	}
	public String checkImage(String equipmentid,String image){
		//byte[] images=FileToByte.readFile(image);
		String images=Base64Util.ImageToBase64(image);
		System.out.println("==================");
		System.out.println(images);
		System.out.println("==================");
		String url=RunWayUtil.URL;
		url=url+RunWayUtil.CHECKIMAGE;
		Map<String, String> map=new HashMap<String, String>();
		map.put("equipmentid", equipmentid);
		map.put("image", images.toString());
		return HttpXmlClient.post(url, map);
	}
	//查询用户数据
	public Object userDate(Integer uid,String phone,String equipmentid,String parkId,String imageId,Integer pageNum,Integer pageSize,Integer type){
		String url=RunWayUtil.URL;
		if(type==null){
			type=0;
		}
		if(type==0){					//查询用户运动记录公园列表
			url=url+RunWayUtil.PARKLIST+"?uid="+uid+"&phone="+phone+"&equipmentid="+equipmentid;
			if(imageId!=null && imageId.length()>0){
				url=url+"&imageId="+imageId;
			}
		}else if(type==1){		//查询用户日/周/月数据及分布
			HashMap<String, Object> map=new HashMap<String, Object>();
			url=url+RunWayUtil.SPORTDATABYPARK+"?uid="+uid+"&phone="+phone+"&equipmentid="+equipmentid+"&parkId="+parkId;
			if(imageId!=null && imageId.length()>0){
				url=url+"&imageId="+imageId;
			}
			String result=HttpXmlClient.get(url);
			//System.out.println(result);
			JSONObject jsonObject = JSONObject.parseObject(result);
			int code=jsonObject.getIntValue("code");
			if(code==200){
				try {
					String strData=jsonObject.getString("data");
					JSONObject strJsonObject = JSONObject.parseObject(strData);
					String todayItem=strJsonObject.getString("todayItem");
					if(todayItem!=null){
						SportDataByPark todayItemPojo=JsonUtil.json2pojo(todayItem, SportDataByPark.class);
						map.put("todayItem", todayItemPojo);
					} 
					String weekItem=strJsonObject.getString("weekItem");
					if(weekItem!=null){
						SportDataByPark weekItemPojo=JsonUtil.json2pojo(weekItem, SportDataByPark.class);
						map.put("weekItem", weekItemPojo);
					} 
					String mothItem=strJsonObject.getString("mothItem");
					if(mothItem!=null){
						SportDataByPark mothItemPojo=JsonUtil.json2pojo(mothItem, SportDataByPark.class);
						map.put("mothItem", mothItemPojo);
					} 
					int weekSportOrder=strJsonObject.getIntValue("weekSportOrder");
					map.put("weekSportOrder", weekSportOrder);
					String userimg=RunWayUtil.IMGURL+strJsonObject.getString("userimg");
					map.put("userimg", userimg);
					return map;
				}catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				return null;
			}
		}else if(type==2){
			url=url+RunWayUtil.THIRTYDAYSHISTORY+"?uid="+uid+"&phone="+phone+"&equipmentid="+equipmentid+"&parkId="+parkId;
			if(pageNum!=null){
				url=url+"&pageNum="+pageNum;
			}
			if(pageSize!=null){
				url=url+"&pageSize="+pageSize;
			}
			String result=HttpXmlClient.get(url);
			//System.out.println(result);
			JSONObject jsonObject = JSONObject.parseObject(result);
			int code=jsonObject.getIntValue("code");
			if(code==200){
				try {
					String strData=jsonObject.getString("data");
					JSONObject strJsonObject = JSONObject.parseObject(strData);
					/*String strData2=strJsonObject.getString("data");
					JSONObject strJsonObject2 = JSONObject.parseObject(strData2);*/
					String strList=strJsonObject.getString("list");
					if(strList!=null && strList.length()>0){
						List<ThirtyDatShiStory> sList=JsonUtil.jsonlists(strList, ThirtyDatShiStory.class);
						if(sList!=null && sList.size()>0){
							for (ThirtyDatShiStory thirtyDatShiStory : sList) {
								thirtyDatShiStory.setDate(thirtyDatShiStory.getDate()!=null ? thirtyDatShiStory.getDate().substring(0,9) : "");
							}
						}
						return sList;
					}else{
						return null;
					}
				}catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
			}else{
				return null;
			}
			
		}
		return HttpXmlClient.get(url);
	}
	public static void main(String[] args) {
		RunWayOperation rwo=new RunWayOperation();
		//System.out.println("=========="+rwo.userDate(244, "15938763139", "384fbf8", "2",null,null,null,1));
		System.out.println("=========="+rwo.userDate(244, "15938763139", "384fbf8", "3",null,1,1000,1));
		//System.out.println("=========="+rwo.returnResult("384fbf8", "2019-01-01", "2020-02-01", 2));
		//System.out.println("=========="+rwo.checkImage("384fbf8", "E:/111.jpg"));
	}
	/*public static String encode(byte[] val){
		ByteArrayOutputStream v1=new ByteArrayOutputStream();
		ByteArrayInputStream v2=new ByteArrayInputStream(val);
		String v4=v1
	}*/
}



